<?php


namespace App\Models\App;


use App\Commons\Utils\ModelTools\BaseModel;
use App\Commons\WechatOpenPlatform\WechatOpenPlatformTool;
use App\Models\AdminUser\User;
use App\Models\Wechat\MiniProgramTemplate;

class AppProject extends BaseModel {
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $searchable = [
        'created_at',
        'name',
        'remark',
        'status',
        'auth_status',
        'mpt_id',
        'app_project_type_id',
        'auth_time',
        'wx_authorizer_appid',
        'wx_nick_name',
        'wx_principal_name',
        'wx_signature',
        'wx_submit_audit_item_rely_mpt',
        'updated_at',
        'switch_audit_success_release',
        'last_audit_status',
    ];

    protected $casts = [
        'wx_submit_audit_item_rely_mpt' => 'integer',
    ];

    protected $hidden = [
        'deleted',
    ];

    /**
     * auth_status  授权状态，0：未授权，1：已授权
     */
    const AUTH_STATUS_YES = 1;
    const AUTH_STATUS_NO  = 0;

    /**
     * type 项目类型，1:小程序,2:公众号
     */
    const TYPE_MINI_PROGRAM = 1;
    const TYPE_GZH          = 2;
    
    /**
     * switch_audit_success_release 审核通过自动发布已通过审核的小程序，1：启用，0：禁用
     */
    const SWITCH_AUDIT_SUCCESS_RELEASE_ON  = 1;
    const SWITCH_AUDIT_SUCCESS_RELEASE_OFF = 0;
    
    /**
     * last_audit_status 最新一次提审单的审核状态,-1:无状态,0:审核成功,1:审核被拒绝,2:审核中,3:已撤回,4:审核延后,
     */
    const LAST_AUDIT_STATUS_NONE    = -1;
    const LAST_AUDIT_STATUS_SUCCESS = 0;
    const LAST_AUDIT_STATUS_FAIL    = 1;
    const LAST_AUDIT_STATUS_WAITING = 2;
    const LAST_AUDIT_STATUS_UNDO    = 3;
    const LAST_AUDIT_STATUS_DELAY   = 4;

    /**
     * 类型获取
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function appProjectType() {
        return $this->belongsTo(AppProjectType::class, 'app_project_type_id', 'id');
    }
    
    /**
     * 项目添加者
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user() {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    /**
     * 模板获取
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function miniProgramTemplate() {
        return $this->hasOne(MiniProgramTemplate::class, 'id', 'mpt_id');
    }

    /**
     * 对ext_json操作
     * @param $val
     */
    public function setExtJsonAttribute($val) {
        $extJson = '{}';
        if (is_array($val) && !empty($val)) {
            $extJson = json_encode($val);
        }

        $this->attributes['ext_json'] = $extJson;
    }

    public function getExtJsonAttribute($val) {
        $extJsonList = json_decode($val, true);

        return is_array($extJsonList) ? $extJsonList : [];
    }

    /**
     * server_domain
     * @param $val
     */
    public function setServerDomainAttribute($val) {
        if (is_array($val)) {
            $serverDomain = json_encode($val);
        } else {
            $serverDomain = json_encode(WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonServerDomain());
        }

        $this->attributes['server_domain'] = $serverDomain;
    }

    public function getServerDomainAttribute($val) {
        $extJsonList = json_decode($val, true);

        return is_array($extJsonList) ? $extJsonList : WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonServerDomain();
    }

    /**
     * webviewdomain
     * @param $val
     */
    public function setWebviewdomainAttribute($val) {
        if (is_array($val)) {
            $serverDomain = json_encode($val);
        } else {
            $serverDomain = json_encode(WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonSetWebViewDomain());
        }

        $this->attributes['webviewdomain'] = $serverDomain;
    }

    public function getWebviewdomainAttribute($val) {
        $extJsonList = json_decode($val, true);

        return is_array($extJsonList) ? $extJsonList : WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonSetWebViewDomain();
    }

    /**
     * wx_submit_audit_item_list
     * @param $val
     */
    public function setWxSubmitAuditItemListAttribute($val) {
        if (is_array($val)) {
            $jsonRes = json_encode($val);
        } else {
            $jsonRes = json_encode(WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonSetWebViewDomain());
        }

        $this->attributes['wx_submit_audit_item_list'] = $jsonRes;
    }

    public function getWxSubmitAuditItemListAttribute($val) {
        $arrRes = json_decode($val, true);

        return is_array($arrRes) ? $arrRes : [];
    }
    
    /**
     * 获取服务器域名,包含类型和模板配置的域名信息
     * @return array
     */
    public function serverDomainWithTypeAndTemplate() {
        $typeServerDomain     = $this->appProjectType->server_domain;
        $templateServerDomain = $this->miniProgramTemplate->server_domain;
        
        $mergeServerDomain = array_map(
            function ($val) {
                return array_unique($val);
            },
            array_merge_recursive(
                $typeServerDomain,
                $templateServerDomain,
                $this->server_domain
            )
        );
        
        return $mergeServerDomain;
    }
    
    /**
     * 业务域名包含类型和模板配置的域名信息
     * @return array
     */
    public function webviewdomainWithTypeAndTemplate() {
        $typeWebviewdomain = $this->appProjectType->webviewdomain;
        $tempWebviewdomain = $this->miniProgramTemplate->webviewdomain;
        
        $mergeWebviewdomain = array_merge(
            $typeWebviewdomain,
            $tempWebviewdomain,
            $this->webviewdomain
        );
        
        return $mergeWebviewdomain;
    }
}